﻿@model GiftCardListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.GiftCards").Text;
}
@Html.AntiForgeryToken()
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-sales.png")" alt="" />
        @T("Admin.GiftCards")
    </div>
    <div class="options">
        <a href="@Url.Action("Create")" class="k-button">@T("Admin.Common.AddNew")</a>
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.ActivatedId):
        </td>
        <td class="adminData">
            @Html.DropDownListFor(model => model.ActivatedId, Model.ActivatedList)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.CouponCode):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.CouponCode)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.RecipientName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.RecipientName)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-giftcards" class="k-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<table class="adminContent">
    <tr>
        <td>
            <div id="giftcards-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#giftcards-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("GiftCardList", "GiftCard"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "AmountStr",
                            title: "@T("Admin.GiftCards.Fields.Amount")"
                        }, {
                            field: "RemainingAmountStr",
                            title: "@T("Admin.GiftCards.Fields.RemainingAmount")"
                        }, {
                            field: "GiftCardCouponCode",
                            title: "@T("Admin.GiftCards.Fields.GiftCardCouponCode")"
                        }, {
                            field: "RecipientName",
                            title: "@T("Admin.GiftCards.Fields.RecipientName")"
                        }, {
                            field: "IsGiftCardActivated",
                            title: "@T("Admin.GiftCards.Fields.IsGiftCardActivated")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsGiftCardActivated#.gif" />'
                        }, {
                            field: "CreatedOn",
                            title: "@T("Admin.GiftCards.Fields.CreatedOn")",
                            width: 200,
                            type: "date",
                            format: "{0:G}"
                        }, {
                            field: "Id",
                            title: "@T("Admin.Common.Edit")",
                            width: 100,
                            template: '<a href="Edit/#=Id#">@T("Admin.Common.Edit")</a>'
                        }]
                    });
                });
            </script>

            <script type="text/javascript">

                $(document).ready(function () {

                    $('#search-giftcards').click(function () {
                        var grid = $('#giftcards-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                        return false;
                    });
                });

                $("#@Html.FieldIdFor(model => model.CouponCode)").keydown(function (event) {
                    if (event.keyCode == 13) {
                        $("#search-giftcards").click();
                        return false;
                    }
                });

                function additionalData() {
                    var data = {
                        ActivatedId: $('#@Html.FieldIdFor(model => model.ActivatedId)').val(),
                        CouponCode: $('#@Html.FieldIdFor(model => model.CouponCode)').val(),
                        RecipientName: $('#@Html.FieldIdFor(model => model.RecipientName)').val()
                    };
                    addAntiForgeryToken(data);
                    return data;
                }

            </script>
        </td>
    </tr>
</table>